package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhy.autolayout.utils.AutoUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.bean.CircleBean;

/**
 * Created by Administrator on 2016/6/28.
 */
   public class MyDraftAdapter extends BaseAdapter{

    private Context context;
    private List<CircleBean.CircleList> shareList;

    public MyDraftAdapter(Context context, List<CircleBean.CircleList> shareList){
        this.context=context;
        this.shareList=shareList;
    }

    @Override
    public int getCount() {
        return shareList != null ? shareList.size():0;
    }

    @Override
    public Object getItem(int i) {
        return shareList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void addView(List<CircleBean.CircleList> list){
        if(list == null || list.size()<=0){
            return;
        }
        shareList.addAll(list);
    }

    public void clear(){
        shareList.clear();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder=null;
        if(view == null){
            view= LayoutInflater.from(context).inflate(R.layout.mydraft_item,null);
            viewHolder=new ViewHolder(view);
            view.setTag(viewHolder);
            AutoUtils.auto(view);
        }else{
            viewHolder= (ViewHolder) view.getTag();
        }
        try {
            viewHolder.mydraft_name.setText(URLDecoder.decode(shareList.get(i).getTitle(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        viewHolder.mydraft_time.setText(shareList.get(i).getCreateTimeStr()+"\n"+shareList.get(i).getAddress());
          String imgs=shareList.get(i).getImgs();
        if(TextUtils.isEmpty(imgs)){
            viewHolder.mydraft_img.setVisibility(View.GONE);
        }
            String getimgs[]=imgs.split(",");
        if(!TextUtils.isEmpty(imgs)){
            List<String> imgList=new ArrayList<String>();
            for(int a=0;a<getimgs.length;a++){
                imgList.add(getimgs[a]);
            }
        }
        Glide.with(context).load(OKUtilAPI.imgBaseUrl+getimgs[0]).into(viewHolder.mydraft_img);

        return view;
    }

    public static class ViewHolder{
        @BindView(R.id.mydraft_img)
        ImageView mydraft_img;
        @BindView(R.id.mydraft_name)
        TextView mydraft_name;
        @BindView(R.id.mydraft_time)
        TextView mydraft_time;
        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
